//
//  LZZLayerTreeManager.m
//  BKDevMenu
//
//  Created by luzz on 2022/6/2.
//

#import "LZZLayerTreeManager.h"

@interface LZZLayerTreeManager()

@property (nonatomic, weak) UIViewController *topVC;

@end

@implementation LZZLayerTreeManager

static LZZLayerTreeManager *sharedInstance = nil;
+ (LZZLayerTreeManager*)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[super allocWithZone:NULL] init];

    });
    return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone
{
    return [self shareManager];
}
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}


- (void)updateCurrentVC:(UIViewController *)topVC
{
    self.topVC = topVC;
}


- (UIColor *)keyColor
{
    return [UIColor systemBlueColor];
}

- (UIColor *)colorWithLevel:(int)level
{
    CGFloat k = (CGFloat)level * 0.1;
    return [UIColor colorWithWhite:0.5 + MIN(k, 0.4)  alpha:1.0];
}


- (NSArray<LZZTreeNode *> *)getCurrentNodes
{
    NSMutableArray *allNodes = [NSMutableArray arrayWithCapacity:1];
    LZZTreeNode *rootNode = [LZZTreeNode nodeWithParentObj:nil nodeObj:[UIApplication sharedApplication] name:@"App" detail:nil depth:0 expand:YES];
    [allNodes addObject:rootNode];
    
    for (int i = 0; i < [UIApplication sharedApplication].windows.count;i++) {

        UIWindow *rootWindow = [UIApplication sharedApplication].windows[i];
        
        if ([rootWindow isKindOfClass:NSClassFromString(@"BKDevWindow")] == NO && rootWindow != self.window) {
            LZZTreeNode *rootWindowNode = [LZZTreeNode nodeWithParentObj:[UIApplication sharedApplication] nodeObj:rootWindow name:NSStringFromClass([rootWindow class]) detail:nil depth:1 expand:YES];
            rootWindowNode.color = [self colorWithLevel:rootWindowNode.depth];
            [allNodes addObject:rootWindowNode];
            
            
            
            if (rootWindow.rootViewController) {
                
                __kindof UIViewController *rootVC = rootWindow.rootViewController;
                LZZTreeNode *rootVCNode = [LZZTreeNode nodeWithParentObj:rootWindow nodeObj:rootVC name:NSStringFromClass([rootVC class]) detail:[NSString stringWithFormat:@"window下的rootVC Title:%@",rootVC.title] depth:2 expand:YES];
                
                if (rootVC == self.topVC) {
                    rootVCNode.color = [self keyColor];
                }else{
                    rootVCNode.color = [self colorWithLevel:rootVCNode.depth];
                }
                
                [allNodes addObject:rootVCNode];
                
                [self deepScanParentObj:rootVC andNodeBox:allNodes level:2];
            
            }
        }
        
        
        
        
    }

    return allNodes;
}

- (void)deepScanParentObj:(UIViewController *)parentObj andNodeBox:(NSMutableArray *)nodeBox level:(int)level
{

    if ([parentObj isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)parentObj;
        for (UIViewController *subVC in nav.viewControllers) {
            LZZTreeNode *subVCNode = [LZZTreeNode nodeWithParentObj:parentObj nodeObj:subVC name:NSStringFromClass([subVC class]) detail:[NSString stringWithFormat:@"Title:%@",subVC.title] depth:level+1 expand:YES];
            if (subVC == self.topVC) {
                subVCNode.color = [self keyColor];
            }else{
                subVCNode.color = [self colorWithLevel:subVCNode.depth];
            }
            [nodeBox addObject:subVCNode];
            [self deepScanParentObj:subVC andNodeBox:nodeBox level:level+1];
        }
    }else if ([parentObj isKindOfClass:[UITabBarController class]]){
        UITabBarController *tabVC = (UITabBarController *)parentObj;
        for (UIViewController *subVC in tabVC.viewControllers) {
            LZZTreeNode *subVCNode = [LZZTreeNode nodeWithParentObj:parentObj nodeObj:subVC name:NSStringFromClass([subVC class]) detail:[NSString stringWithFormat:@"Title:%@",subVC.title] depth:level+1 expand:YES];
            if (subVC == self.topVC) {
                subVCNode.color = [self keyColor];
            }else{
                subVCNode.color = [self colorWithLevel:subVCNode.depth];
            }
            [nodeBox addObject:subVCNode];
            [self deepScanParentObj:subVC andNodeBox:nodeBox level:level+1];
        }
    }
}

@end
